package bank;

import java.util.Arrays;

public class _59GenerateMatrix {
    static int[][] matrix;
    public static int[][] generateMatrix(int n) {
        matrix = new int[n][n];
        matrixHelper(0,0,1,n);
        return matrix;
    }

    private static void matrixHelper(int x, int y, int start, int n) {
        if (n <= 0){
            return;
        }
        if (n == 1){
            matrix[x][y] = start;
        }
        for (int i = x;i < x+n-1;i++){
            matrix[y][i] = start++;
        }
        for (int i = y;i < y+n-1;i++){
            //  注意此处的列下标 为什么是 x+n-1
            matrix[i][x+n-1] = start++;
        }
        for (int i = x+n-1;i > x;i--){
            matrix[y+n-1][i] = start++;
        }
        for (int i = y+n-1;i > x;i--){
            matrix[i][x] = start++;
        }
        matrixHelper(x+1,y+1,start,n-2);
    }
}
